#ifndef __can_Cfg_h__
#define __can_Cfg_h__

/**********************************************************************************
 *@file    : mid_Cfg.h 
 *@brief   : can ?? head file
 *@author  : mcu
 *@version : Ver 1.1
 *@data    : 2018-03-23
 *@note    : Copyright(c) 2018 JJI. All rights reserved.
 ***********************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "mid_S32kToAutoChip_Hal.h"

#include "types.h"
//#include "flexcan_driver.h"
/* Private define ------------------------------------------------------------*/
#include "CanTypes.h"
//CAN???
#define CAN0CH 0
#define CAN1CH 1
#define CAN2CH 2

#define    CAN_STD_ID    0
#define    CAN_EXT_ID    1


//-----------------------------end Can register -----------------------------------------

/* Private typedef -----------------------------------------------------------*/
/*! @brief FlexCAN message buffer structure
 * Implements : flexcan_msgbuff_t_Class
 */

typedef void (* can_callback_t)(uint8_t instance, flexcan_msgbuff_t *msg);

#if 0
#define	CAN0_EN_OUTPUT		        (GPIO_SetDir(GPIO_OUT_CAN_EN, GPIO_OUT),\
                                    GPIO_SetPullup(GPIO_OUT_CAN_EN,ENABLE))
#define	CAN0_EN_EN                  GPIO_SetPinLevel(GPIO_OUT_CAN_EN, GPIO_LEVEL_HIGH)
#define	CAN0_EN_DIS			        GPIO_SetPinLevel(GPIO_OUT_CAN_EN, GPIO_LEVEL_LOW)

#define	CAN0_STB_OUTPUT			    (GPIO_SetDir(GPIO_OUT_CAN_STB, GPIO_OUT),\
                                    GPIO_SetPullup(GPIO_OUT_CAN_STB,ENABLE))
#define	CAN0_STB_EN			        GPIO_SetPinLevel(GPIO_OUT_CAN_STB, GPIO_LEVEL_HIGH)
#define	CAN0_STB_DIS			    GPIO_SetPinLevel(GPIO_OUT_CAN_STB, GPIO_LEVEL_LOW)

#define	CAN0_WAKE_OUTPUT		    __NOP()
#define	CAN0_WAKE_DIS			    __NOP()
#define	CAN0_WAKE_EN			    __NOP()

#define	CAN0_FAULT_INPUT		    (GPIO_SetDir(GPIO_IN_CAN_FAULT, GPIO_IN),\
                                     GPIO_SetPullup(GPIO_IN_CAN_FAULT,ENABLE))
#define	CAN0_FAULT_IS_HIGH           GPIO_GetPinLevel(GPIO_IN_CAN_FAULT)
#else
#define	CAN0_EN_OUTPUT		       (GPIO_SetFunc(GPIO_OUT_CAN_EN, GPIO_FUN0),\
                                    GPIO_SetDir(GPIO_OUT_CAN_EN, GPIO_OUT),\
                                    GPIO_SetPullup(GPIO_OUT_CAN_EN,ENABLE))
#define	CAN0_EN_EN                  GPIO_SetPinLevel(GPIO_OUT_CAN_EN, GPIO_LEVEL_HIGH)
#define	CAN0_EN_DIS			        GPIO_SetPinLevel(GPIO_OUT_CAN_EN, GPIO_LEVEL_LOW)

#define	CAN0_STB_OUTPUT			   (GPIO_SetFunc(GPIO_OUT_CAN_STB, GPIO_FUN0),\
                                    GPIO_SetDir(GPIO_OUT_CAN_STB, GPIO_OUT),\
                                    GPIO_SetPullup(GPIO_OUT_CAN_STB,ENABLE))
#define	CAN0_STB_EN			        GPIO_SetPinLevel(GPIO_OUT_CAN_STB, GPIO_LEVEL_HIGH)
#define	CAN0_STB_DIS			    GPIO_SetPinLevel(GPIO_OUT_CAN_STB, GPIO_LEVEL_LOW)

#define	CAN0_WAKE_OUTPUT		    __NOP()
#define	CAN0_WAKE_DIS			    __NOP()
#define	CAN0_WAKE_EN			    __NOP()

#define	CAN0_FAULT_INPUT		    (GPIO_SetFunc(GPIO_IN_CAN_FAULT, GPIO_FUN0),\
                                     GPIO_SetDir(GPIO_IN_CAN_FAULT, GPIO_IN),\
                                     GPIO_SetPullup(GPIO_IN_CAN_FAULT,ENABLE))
#define	CAN0_FAULT_IS_HIGH           GPIO_GetPinLevel(GPIO_IN_CAN_FAULT)
#endif


typedef struct tag_struct_canMsg  /*  CAN message structure   */
{
	uint8_t  Code; 					/* Received message buffer code */
	uint8_t  Dlc;            		/* Recieved message number of data bytes */
	uint8_t  IdType;
	uint32_t  Id;                		/* Received message ID */
	uint8_t   Data[8];           	    /* Received message data string*/
	uint32_t  Timestamp;         		/* Received message time */
} struct_canMsg_t;

typedef enum
{
	CAN_OPERATING_UNINIT = 0,
	CAN_OPERATING_WORKING,
    CAN_OPERATING_SLEEP
} Can_workState_t;

typedef enum
{
	CAN_ERROR,  // error
	CAN_NORMAL,	// normal
	CAN_BUSOFF
}CAN_STATUS;

typedef struct  
{
    uint8_t detectTime;    
    uint8_t swCtrlStep; 
	CAN_STATUS status;
	uint8_t occurCnt;
	uint8_t occurFlag;  //occurCnt > 5
	uint8_t DtcStatus;
	uint32_t recoverTimeCnt;
	uint32_t timeoutCnt;
}struct_CanBusoff_t;

typedef struct CANState{
	Can_workState_t				workState;
	struct_CanBusoff_t			busOffState;
	uint8_t						CommunicationEnabled;
	
	can_callback_t				rxCallBack;
	struct_canMsg_t				*rxBuff;
	uint16_t					rxBuffSize;
	volatile uint16_t			rxInPtr;
	uint16_t					rxOutPtr;
	
	can_callback_t				txCallBack;
	struct_canMsg_t				*txBuff;
	uint16_t					txBuffSize;
	uint16_t					txInPtr;
	uint16_t					txOutPtr;
	boolean                     isTxBusy;
	uint32_t                    txPendingTimeLeft;

	can_callback_t				wakeUpCallBack;
	void (*busOffCallBack)(uint8_t instance);

	uint8_t						appPdu[8];
	uint32_t					appPduCycleMessageTimeLeft;
	int 						timehold;
} can_state_t;

/*---------------------------------------------------------------------------------------------------------*/

typedef struct {

//	can_callback_t					rxCallBack;
	struct_canMsg_t					*rxBuff;
	uint16_t						rxBuffSize;

//	can_callback_t					txCallBack;
	struct_canMsg_t					*txBuff;
	uint16_t						txBuffSize;

//	can_callback_t					wakeUpCallBack;
//	void (*busOffCallBack)(void);
} can_user_config_t;


#define TX_CANID_STD_MASK 0x000007FF
#define TX_CANID_EXT_MASK 0x1FFFFFFF

/* ================================================================================== */
/* =========================== CAN0 message start============================================ */
//send
#define DIAG_RESP		   0x7D2
#define DMS_1              0x40F
#define DMS_2              0x3FD
#define DMS_3              0x44D

//receive
#define DIAG_PHYS		    0x752
#define DIAG_FUNC		    0x7DF
#define TIHU_IHU_11	        0x45A
#define ABS_ESP_G	        0x2C0
#define BCM_1	            0x5F0
#define SAM_1_G	            0x39C
#define ESC_2	            0xA8
#define IHU_13	            0x313
#define SAM_1	            0x12B

extern uint32_t sg_can0FilterMaskIdList[];
extern const can_user_config_t can0_InitConfig;


/* Private functions ---------------------------------------------------------*/



#endif



